package com.cqcvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cqcvc.entity.Permission;
import com.cqcvc.entity.User;
import com.cqcvc.mapper.PermissionMapper;
import com.cqcvc.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqcvc.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author hunter
 * @since 2022-08-04
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Set<String> getPermissionByUsername(String username) {
        //得到用户对应的权限
        return permissionMapper.selectPermissionByUsername(username);
    }

    @Override
    public List<Permission> selectPermission(String permissionName,Integer isMenu) {
        QueryWrapper qw=new QueryWrapper();
        if(!StringUtil.isNullOrEmpty(permissionName)){
            qw.like("permission_name",permissionName);
        }
        if(!StringUtil.isNullOrEmpty(isMenu)){
            qw.eq("is_menu",isMenu);
        }
        List<Permission> list=permissionMapper.selectList(qw);
        return  list;
    }

    @Override
    public Permission selectPermissionById(Integer id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public int savePermission(Permission permission) {
        if(StringUtil.isNullOrEmpty(permission.getPermissionId())){
            permissionMapper.insert(permission);
        }else{
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("permission_id",permission.getPermissionId());
            permissionMapper.update(permission,updateWrapper);
        }
        System.out.println("插入或编辑的菜单编号:"+permission.getPermissionId());
        return permission.getPermissionId();
    }

    @Override
    public int delPermission(Integer id) {
        return permissionMapper.deleteById(id);
    }

    @Override
    public Integer batchDelPermission(List<Integer> ids) {
        return permissionMapper.deleteBatchIds(ids);
    }
}
